﻿using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using CodeLibrary.IO.Utils;

namespace CodeLibrary.Controls.PropertyGrid.PropertyGridEditors
{
    public class OpenFolderEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            var edSvc = (IWindowsFormsEditorService) provider.GetService(typeof (IWindowsFormsEditorService));
            if (edSvc != null)
                value = DirectoryUtils.SelectDirectory((string) value);
            return value ?? "";
        }
    }
}